$(function () {
    ajaxQueryPopupSloganList(1);
    $('body').on('click', '#btn1', function () { // 搜索
        ajaxQueryPopupSloganList(1);
    }).on('click', '#createPopupSlogan', function () {
        createPopupSlogan(0);
    }).on("click", ".fa-trash-o", function () {//删除
        var obj = $(this).parent('a');
        var id = obj.data("id");
        updatePopupSloganStatus(id, 2);
    }).on("click", ".fa-hourglass-start", function () {//立即开始
        var obj = $(this).parent('a');
        var id = obj.data("id");
        updatePopupSloganStatus(id, 3);
    }).on("click", ".fa-pause", function () {//停止
        var obj = $(this).parent('a');
        var id = obj.data("id");
        updatePopupSloganStatus(id, 4);
    }).on("click", ".fa-edit", function () {//修改
        var obj = $(this).parent('a');
        var id = obj.data("id");
        createPopupSlogan(id);
    });
})

function ajaxQueryPopupSloganList(pageNo) {
    var params = $("#search-form").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/planTools/popupSlogan/ajaxQueryPopupSloganList',//请求URL
        contextId: 'popupSlogan-list-context',//响应内容页面设置内容ID
        params: params,//请求参数
        pageNo: pageNo,//页码
        pageCall: ajaxQueryPopupSloganList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId: '', //全选框ID
        callback: function () {//数据加载完成回调
            $("#count").html('（共' + total + '个弹窗广告）');
            // tooltips
            $("[data-toggle=popover]").popover();
        }
    };
    ajaxGetListData(obj);
}

//创建弹窗广告
function createPopupSlogan(id) {
    parent.layer.open({
        type: 2,
        area: winArea,
        skin: 'layui-layer-demo',
        title: '弹窗广告设置',
        content: baseloaction + "/admin/planTools/popupSlogan/initAdd?id=" + id,
        btn: ['保存', '取消'],
        btnAlign: 'c',
        maxmin: true,
        resize: true,
        yes: function (index, layero) {
            var iframeName = layero.find("iframe").attr('name');
            var params = $(parent.window.frames[iframeName].document).find("#createPopupSloganFrom").serialize();
            var layBtn = layero.find('.layui-layer-btn0');
            //layBtn.addClass('btn-disabled').html('<i class="fa fa-circle-o-notch"></i> 正在新建中...')
            $.ajax({
                url: baseloaction + "/admin/planTools/popupSlogan/savePopupSlogan",
                data: params,
                dataType: "json",
                type: "post",
                success: function (result) {
                    if (result.success == true) {
                        ajaxQueryPopupSloganList(1)
                        parent.layer.close(index);
                    } else {
                        parent.icModale(0, result.message);
                    }
                }
            });
        }
    });
}

function updatePopupSloganStatus(id, status) {
    var message = "";
    if (status == 2) {
        message = "确认要删除选中数据？";
    }
    if (status == 3) {
        message = "确认要立即开始该弹窗吗？";
    }
    if (status == 4) {
        message = "确认要停止该弹窗吗？";
    }
    icModale(2, message, 3, function () {
        $.ajax({
            url: baseloaction + "/admin/planTools/popupSlogan/updatePopupSloganStatus",
            data: {"id": id, "status": status},
            type: "post",
            dataType: "json",
            success: function (result) {
                icModale(0, result.message);
                if (result.success) {
                    if (2 == status) {
                        $("#popupSlogan-" + id).remove();
                    } else {
                        var pageNum = $("#pageNum").val();
                        setTimeout(function () {
                            ajaxQueryPopupSloganList(pageNum);
                        }, 1000);
                    }
                }
            }, error: function (error) {
                icModale(0, "系统繁忙,操作失败");
            }
        })
    })
}